﻿using Entity.Package;
using Services;
using System;
using System.Text;
using Tools;

namespace BLL.Business
{
    public class StrikeBalance
    {
        /// <summary>
        ///  冲帐业务
        /// </summary>
        public static void ProcessStrikeBalance(PackRequestStrikeBalance detail, CSocket.ClientThread client)
        {
            try
            {
                var ser = IAgentPayment.GetAgentPaymentService();
                var pkStrikeReturn = new PackReturnStrikeBalance();
                pkStrikeReturn.TradeCode = ((int)TradeCode.缴费冲帐).ToString();

                #region 暂不支持销账
                ////查询交易是否存在
                //var querReqData = new { userId = detail.GasUserID.Trim(), bankWaterNum = detail.BankWaterNumber.Trim() };
                //var queryResult = ser.QueryOrder(querReqData);

                //if (queryResult.Code == Entity.Service.ResultType.Success)
                //{
                //    var queryResData = queryResult.Data;
                //    //交易状态为 已支付
                //    if (queryResData["orderCode"].ToString() == "1")
                //    {
                //        //支付冲正
                //        var reversalReqData = new { userId = detail.GasUserID.Trim(), bankWaterNum = detail.BankWaterNumber.Trim(), orderWaterNum = queryResData["orderWaterNum"].ToString().Trim() };
                //        var reversalResult = ser.Reversal(reversalReqData);
                //        if (reversalResult.Code == Entity.Service.ResultType.Success)
                //        {
                //            pkStrikeReturn.ResponseCode = ((int)ResponseCode.交易成功).ToString().PadLeft(3, '0');
                //            pkStrikeReturn.GasUserID = detail.GasUserID.Trim();
                //            pkStrikeReturn.BankWaterNumber = detail.BankWaterNumber;
                //            pkStrikeReturn.PayMoney = queryResData["payMoney"].ToString();
                //            LogHelper.WriteInfoLog("冲帐成功，冲帐包内容为：" + detail.ToString());
                //        }
                //        else
                //        {
                //            pkStrikeReturn.ResponseCode = ((int)Common.GetNoSuccessCode(reversalResult.Code)).ToString().PadLeft(3, '0');
                //            pkStrikeReturn.GasUserID = detail.GasUserID.Trim();
                //            pkStrikeReturn.PayMoney = "";
                //        }
                //    }
                //    else
                //    {
                //        pkStrikeReturn.ResponseCode = ((int)ResponseCode.交易失败).ToString().PadLeft(3, '0');
                //        pkStrikeReturn.GasUserID = detail.GasUserID.Trim();
                //        pkStrikeReturn.PayMoney = "";
                //    }
                //}
                //else
                //{
                //    pkStrikeReturn.ResponseCode = ((int)Common.GetNoSuccessCode(queryResult.Code)).ToString().PadLeft(3, '0');
                //    pkStrikeReturn.GasUserID = detail.GasUserID.Trim();
                //    pkStrikeReturn.PayMoney = "";
                //}

                //pkStrikeReturn.PackHead = new PackHead(detail.packHead.PayWay, detail.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkStrikeReturn.ToString()).Length).ToString());
                //byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkStrikeReturn);
                //if (client.Send(sendPackage))  //返回包发送成功，断开链接
                //    client.CloseSocket();
                //LogHelper.WriteInfoLog(string.Format("{0}用户的{1}冲正调用结束！响应码：{2}", detail.GasUserID.Trim(), detail.packHead.BankWaterNumber, pkStrikeReturn.ResponseCode));

                #endregion

                pkStrikeReturn.ResponseCode = ((int)ResponseCode.交易失败).ToString().PadLeft(3, '0');
                pkStrikeReturn.GasUserID = detail.GasUserID.Trim();
                pkStrikeReturn.PayMoney = "";
                pkStrikeReturn.PackHead = new PackHead(detail.packHead.PayWay, detail.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkStrikeReturn.ToString()).Length).ToString());
                byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkStrikeReturn);
                if (client.Send(sendPackage))  //返回包发送成功，断开链接
                    client.CloseSocket();
                LogHelper.WriteInfoLog($"暂不支持{detail.GasUserID}用户冲正调用");

            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ProcessStrikeBalance方法-冲帐业务出现异常，异常信息：" + ex.Message, ex);
                PackFailReturn pkFailReturn = new PackFailReturn();
                pkFailReturn.TradeCode = ((int)TradeCode.缴费冲帐).ToString();
                pkFailReturn.ResponseCode = ((int)ResponseCode.其他错误).ToString().PadLeft(3, '0');
                pkFailReturn.PackHead = new PackHead(detail.packHead.PayWay, detail.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
            }
        }
    }
}
